package com.team.student.service.impl;

import com.team.student.entity.Student;
import com.team.student.entity.StudentExample;
import com.team.student.mapper.StudentMapper;
import com.team.student.service.StudentService;
import com.team.student.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

public class StudentServiceImpl implements StudentService{

    @Override//查所有
    public List<Student> getStudent() {
        try {
        SqlSession session = MyBatisUtil.getSession();
       StudentMapper sm=session.getMapper(StudentMapper.class);
        List<Student>li=sm.selectByExample(new StudentExample());
        return li;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return null;
    }

    @Override//添加
    public int addStudent(Student student) {
        int a=-1;
        try {
        SqlSession ss=MyBatisUtil.getSession();
        StudentMapper mapper = ss.getMapper(StudentMapper.class);
        a= mapper.insertSelective(student);
        ss.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return a ;
    }

    @Override//删除
    public int deleteStudent(Integer xh) {
        int b=-1;
        try {
        SqlSession ss=MyBatisUtil.getSession();
        StudentMapper mapper = ss.getMapper(StudentMapper.class);
        b= mapper.deleteByPrimaryKey(xh);
        ss.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return b;
    }

    @Override//查单条
    public Student selectStudent(Integer xh) {
        SqlSession ss=MyBatisUtil.getSession();
        try {
        StudentMapper mapper = ss.getMapper(StudentMapper.class);
        Student stu=mapper.selectByPrimaryKey(xh);
            return stu;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return null;
    }

    @Override//修改
    public int updStudent(Student student) {
        int c=-1;
        try {
        SqlSession ss=MyBatisUtil.getSession();
        StudentMapper mapper = ss.getMapper(StudentMapper.class);
        c= mapper.updateByPrimaryKeySelective(student);
        ss.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return c;
    }
}
